/*
***************************************************************************
//  File........: Team02_logger.c
//
//  Author(s)...: Group02: Dilip
//
//  Target(s)...: AVR Butterfly running ATmega169 
//
//  Compiler....: AVR Studio
//
//  Description.: Menu Body for Temperature logger Project from Team 02 (Christian, Dilip, Emanuel, Mishaal, Shorya, Victor) SS2013
//
//  Revisions...: 1.0
//
//  YYYYMMDD - VER. - COMMENT                                       - SIGN.
//
//  20130620 - 1.0  - Created                                       - DDC
//
//***************************************************************************
*/

#include "dataflash.h"
#include <avr/io.h>
#include <easybutterfly.h>

//Inputs: PB4(center), PB6(up), PB7(down), PE2(left) and PE3(right)


//Main Function Begins here

int main (void) 
{
//*********************----VARIABLES and Initialisation----*************************
LCD_Init();

//Inputs: PB4(center), PB6(up), PB7(down), PE2(left) and PE3(right)
	DDRB	= DDRB	& 0b00101111;
	DDRE	= DDRE	& 0b11110011;
/// switch on pull up resistors
	PORTB	= PORTB	| 0b11010000;
	PORTE	= PORTE	| 0b00001100;

//This state flag should be checked for controlling the logging
int state_ctrl = 0; 					//Default logging stopped mode: 0 for Logging stopped
int menu_ctrl = 1;						//Menu selection index
int scroll_ctrl = 1;					//scroll selection index for main menu
int sub_menu_scroll_ctrl = 1;			//scroll selection index for sub menu

//Button_push may be used for power saver in which case it should be appropriately set after inner loops
int button_push = 1;					//flag for button check
int hmtoggle = 1;						//Hrs Min Toggle for interval setting, 1 corresponds to min

//Flag for Light intensity mode. 1 corresponds to ON.
int light_toggle=1;

// the values of h and m should be used for interval calculation
uint8_t h,m;							//variables to store hrs and min for the interval rate
h=00;									//default rate 0h5m. these variables will be used in the functions
m=05;

char min[4], hrs[4], hh[4], mm[4], time[15]; //Character strings to store and display interval rate

strcpy(min, "m");						// Storing units(indices) for the min and hrs strings in the clock 
strcpy(hrs, "h");

char message2_1[25];
strcpy(message2_1, "2.1 Current Interval ");

//LCD_puts(state_ctrl);
//LCD_puts("Start Displaying on LCD");
//delay_ds(0.5);

while(1)
{										//endless loop



//************************************************************************************************************
//Start of Main Menu-----
//**********************************************************************************************************
while(menu_ctrl==1)                     //Main Menu index
	{

	if(scroll_ctrl==1)					//Main Menu Option 1
		{
		if(state_ctrl==0)				//state_ctrl=0 when logging stopped
			{
			LCD_puts("1.Start Log");	//Menu Option 1 in logging stopped mode 
			//delay_ds(200);
			}
		else if (state_ctrl==1)			//state_ctrl=1 for logging active
			{
			LCD_puts("1.Stop Log");		//Menu Option 1 in logging running mode
			}
		}
	
	else if(scroll_ctrl==2)				//Main Menu Option 2
		{
		
		LCD_puts("2.Define Interval");
		//delay_ds(200);
		}

	
	else if(scroll_ctrl==3)				//Main Menu Option 3
		{
		
		LCD_puts("3.Read Log");

		} 

	else if(scroll_ctrl==4)				//Main Menu Option 4
		{
		
		LCD_puts("4.Send to PC");

		} 

	else if(scroll_ctrl==5)				//Main Menu Option 5
		{
		if(light_toggle==1)				//Checking for Light Intensity Triggering on/off: 1 corresponds to ON
			{
			LCD_puts("5.Turn Off Light Trigger");
			}
		else if(light_toggle==0)
			{
			LCD_puts("5.Turn ON Light Trigger");
			}

		} 


	while(button_push==1)						//checking for scrolling actions
		{
		while(!(PINB & (1 << 7)))				//Joystick down
			{
			CLEAR_LCD
			delay_ds(50);
			scroll_ctrl++;
			button_push=0;						//button check loop exit criteria
			}

		while(!(PINB & (1 << 6)))				//Joystick up
			{	
			CLEAR_LCD
			delay_ds(50);
			scroll_ctrl--;
			button_push=0;						//button check loop exit criteria
			}
		
		
		while(!(PINB & (1 << 4)))				//Center Button
			{
			delay_ds(50);
			CLEAR_LCD
			button_push=0;						//button check loop exit criteria
			
			if(scroll_ctrl==1)					//selecting first menu option i.e logging
				{
				menu_ctrl=2;
				state_ctrl = (!state_ctrl);
				}
			else if(scroll_ctrl==2)					//selecting 2nd option i.e changing interval
				{
				menu_ctrl=3;						
				}
			else if(scroll_ctrl==3)					//selecting 3rd option i.e reading log
				{
				menu_ctrl=4;						
				}
			else if(scroll_ctrl==4)					//selecting 4th option i.e UART
				{
				menu_ctrl=5;
				}
			else if(scroll_ctrl==5)					//selecting 5th option
				{
				light_toggle=!light_toggle;
				}
			
			}
		
		}
		
	button_push=1;								//resetting button check loop exit criteria

	if(scroll_ctrl<1)							//controlling scroll limits
		{
		scroll_ctrl=1;
		}

	else if(scroll_ctrl>5)
		{
		scroll_ctrl=5;
		}


		//LCD_NUMBER(scroll_ctrl)				//LINE FOR DEBUGGING

	}

//LCD_puts(menu_ctrl);
//delay_ds(300);

//**************************************************************************************************************
//start of 1st sub menu: Start/Stop Log
//**************************************************************************************************************
while(menu_ctrl==2)								//Sub Menu Index
	{

	if(sub_menu_scroll_ctrl==1)					//Sub Menu Option 1
		{
		if(state_ctrl==1)						//Display interval if logging is active
			{
			strcpy(time, "");								//resetting or emptying the string
			strcpy(message2_1, "2.1 CurrentInterval ");	//resetting the string
			itoa(h, hh, 10);								//converting the value in h into a char string hh
			strcat(hh, hrs);								//appending 'H' at the end of digits in hh

			itoa(m, mm, 10);								//converting the value in m into a char string mm
			strcat(mm, min);								//appending 'M' at the end of digits in mm

			strcat(time, hh);								//storing hh value with a trailing 'H' in the timestamp
			strcat(time, mm);								//appending mm value with a trailing 'M' in the timestamp
			//LCD_Colon(2);

			//LCD_puts("1.1 Current Interval:"); 			//add the current interval variable here
			strcat(message2_1,time);
			LCD_puts(message2_1);							//Displaying Time stamp message
			
			}

		else if(state_ctrl==0)					//Display Log stop Message
			{
			LCD_puts("1.1 Logging Stopped");
			}
		}

	else if(sub_menu_scroll_ctrl==2)			//Sub Menu Option 2
		{
		LCD_puts("1.2 Define Interval");			// add code to navigate back to 3rd menu
		}
/* replaced with left scroll functionality
	else if(sub_menu_scroll_ctrl==3) 			//Sub Menu Option 3
		{
		LCD_puts("1.3 Back");								
		}
*/
	while(button_push==1)								//checking for scrolling actions
		{
		while(!(PINB & (1 << 7)))						//Joystick down
			{
			CLEAR_LCD
			delay_ds(50);
			sub_menu_scroll_ctrl++;
			button_push=0;
			}

		while(!(PINB & (1 << 6)))						//Joystick down
			{
			CLEAR_LCD
			delay_ds(50);
			sub_menu_scroll_ctrl--;
			button_push=0;								//button check loop exit criteria
			}
		
		while(!(PINB & (1 << 4)))						//Center Button
			{
			delay_ds(50);
			CLEAR_LCD
			button_push=0;								//button check loop exit criteria
			
/* replaced with left scroll functionality
			if(sub_menu_scroll_ctrl==3)					//selecting 3rd option
				{
				menu_ctrl=1;
				sub_menu_scroll_ctrl = 1;			//setting submenu scroll to first position
				}
*/
			if(sub_menu_scroll_ctrl==2)					//selecting 2rd option
				{
				menu_ctrl=3;
				sub_menu_scroll_ctrl = 1;			//setting submenu scroll to first position
				}
			}
		
		while(!(PINE & (1 << 2)))						//Left Button 
			{
			delay_ds(50);
			menu_ctrl=1;								//Return to main Menu
			sub_menu_scroll_ctrl = 1;				
			button_push=0;								//button check loop exit criteria
			}
				

		}
		
	button_push=1;										//resetting button check loop exit criteria

	if(sub_menu_scroll_ctrl<1)							//controlling scroll limits
		{
		sub_menu_scroll_ctrl=1;
		}

	else if(sub_menu_scroll_ctrl>2)
		{
		sub_menu_scroll_ctrl=2;
		}

	
	}

//**************************************************************************************************************
//start of 3rd sub menu: Change Interval
//**************************************************************************************************************
while(menu_ctrl==3)										//Sub Menu Index
	{

	if(sub_menu_scroll_ctrl==1)							//Sub Menu Option 1
		{
		strcpy(time, "");								//resetting or emptying the string
		itoa(h, hh, 10);								//converting the value in h into a char string hh
		strcat(hh, hrs);								//appending 'H' at the end of digits in hh

		itoa(m, mm, 10);								//converting the value in m into a char string mm
		strcat(mm, min);								//appending 'M' at the end of digits in mm

		strcat(time, hh);								//storing hh value with a trailing 'H' in the timestamp
		strcat(time, mm);								//appending mm value with a trailing 'M' in the timestamp
		//LCD_Colon(2);

		LCD_puts(time);									//Displaying Time stamp
		LCD_UpdateRequired(TRUE, 0);					//Disabling LCD Scroll Mode

		//LCD_puts("2.1 HHMM");				//add the current interval variable here
		}
/*  replaced with left scroll functionality
	else if(sub_menu_scroll_ctrl==2) 					//Sub Menu Option 2
		{
		LCD_puts("2.2 Back");								
		
		}
*/
	while(button_push==1)								//checking for scrolling actions
		{
		while(!(PINB & (1 << 7)))						//Joystick down
			{
			delay_ds(50);
			CLEAR_LCD
			button_push=0;								//button check loop exit criteria
			
			if(sub_menu_scroll_ctrl==1)					//Setting interval in first option
				{
				if(hmtoggle==1)							//setting minutes
					{
					m=m-1;
					if(m<1)							//resetting min to 0 after 59
						{
						m=59;
						}
					}
			
				else if(hmtoggle==0)					//setting hrs
					{
					h=h-1;
					if(h>24)							//resetting hrs to 0 after 24
						{
						h=24;
						}
					}
				//sub_menu_scroll_ctrl = 1;			//setting submenu scroll to first position
				}
			}

		while(!(PINB & (1 << 6)))						//Joystick up
			{delay_ds(50);
			CLEAR_LCD
			button_push=0;								//button check loop exit criteria
			
			if(sub_menu_scroll_ctrl==1)					//Setting interval in first option
				{
				if(hmtoggle==1)							//setting minutes
					{
					m=m+1;
					if(m>59)							//resetting min to 0 after 59
						{
						m=0;
						}
					}
			
				else if(hmtoggle==0)					//setting hrs
					{
					h=h+1;
					if(h>24)							//resetting hrs to 0 after 24
						{
						h=0;
						}
					}
				//sub_menu_scroll_ctrl = 1;			//setting submenu scroll to first position
				}
			}
/*		
		while(!(PINB & (1 << 4)))						//Center Button
			{
			delay_ds(50);
			CLEAR_LCD
			button_push=0;								//button check loop exit criteria
			
			if(sub_menu_scroll_ctrl==1)					//Setting interval in first option
				{
				if(hmtoggle==1)							//setting minutes
					{
					m=m+1;
					if(m>59)							//resetting min to 0 after 59
						{
						m=0;
						}
					}
			
				else if(hmtoggle==0)					//setting hrs
					{
					h=h+1;
					if(h>24)							//resetting hrs to 0 after 24
						{
						h=0;
						}
					}
				sub_menu_scroll_ctrl = 1;			//setting submenu scroll to first position
				}
			
 replaced with left scroll functionality
			else if(sub_menu_scroll_ctrl==2)					//selecting 2nd option
				{
				menu_ctrl=1;
				sub_menu_scroll_ctrl = 1;			//setting submenu scroll to first position
				LCD_UpdateRequired(TRUE, 1);					//Enabling LCD Scroll Mode
				}
				


			}
*/
		while(!(PINE & (1 << 2)))						//Left Button 
			{
			delay_ds(50);
			menu_ctrl=1;								//Return to main Menu
			sub_menu_scroll_ctrl = 1;				
			button_push=0;								//button check loop exit criteria
			scroll_ctrl=2;
			}

		while(!(PINE & (1 << 3)))						//Right Button
			{
			delay_ds(50);
			//button_push=0;								//button check loop exit criteria
			if(sub_menu_scroll_ctrl==1)
				{
				hmtoggle=!hmtoggle;
				}
			}
		}

	
		
	button_push=1;										//resetting button check loop exit criteria

	if(sub_menu_scroll_ctrl<1)							//controlling scroll limits
		{
		sub_menu_scroll_ctrl=1;
		}

	else if(sub_menu_scroll_ctrl>1)
		{
		sub_menu_scroll_ctrl=1;
		}

	
	}							//End of Submenu

//***********************************************************************************************************
// 4th Submenu: Read Log
//************************************************************************************************************

while(menu_ctrl==4)										//Sub Menu Index
	{

	//TO BE REPLACED BY ACTUAL RECORD DATA AND A CONDITION TO DISPLAY THE BELOW PROMPT WHEN NEEDED
	LCD_puts("No Records to Display"); 					


/*	if(sub_menu_scroll_ctrl==1)							//Sub Menu Option 1
		{
		}
*/
	while(button_push==1)								//checking for scrolling actions
		{

		while(!(PINB & (1 << 7)))						//Joystick down
			{
			delay_ds(50);
			CLEAR_LCD
			button_push=0;								//button check loop exit criteria
			//Fetch Next record here
			}

		while(!(PINB & (1 << 6)))						//Joystick up
			{
			delay_ds(50);
			CLEAR_LCD
			button_push=0;								//button check loop exit criteria
			//Fetch Previous Record Here
			}

		while(!(PINE & (1 << 2)))						//Left Button 
			{
			delay_ds(50);
			menu_ctrl=1;								//Return to main Menu
			button_push=0;								//button check loop exit criteria
			scroll_ctrl=3;
			}

		}

	
		
	button_push=1;										//resetting button check loop exit criteria

	if(sub_menu_scroll_ctrl<1)							//controlling scroll limits
		{
		sub_menu_scroll_ctrl=1;
		}

	else if(sub_menu_scroll_ctrl>1)
		{
		sub_menu_scroll_ctrl=1;
		}

	
	}												//End of Submenu

//**************************************************************************************************************
// 5th Submenu: Send to PC
//************************************************************************************************************

while(menu_ctrl==5)										//Sub Menu Index
	{

	LCD_puts("Sending to PC"); 					

//Initiate UART Communication here using menu_ctrl=5

/*	if(sub_menu_scroll_ctrl==1)							//Sub Menu Option 1
		{
		}
*/
/*	while(button_push==1)								//checking for scrolling actions
		{

		while(!(PINB & (1 << 7)))						//Joystick down
			{
			delay_ds(50);
			CLEAR_LCD
			button_push=0;								//button check loop exit criteria
			//Fetch Next record here
			}

		while(!(PINB & (1 << 6)))						//Joystick up
			{
			delay_ds(50);
			CLEAR_LCD
			button_push=0;								//button check loop exit criteria
			//Fetch Previous Record Here
			}
*/
		while(!(PINE & (1 << 2)))						//Left Button 
			{
			delay_ds(50);
			menu_ctrl=1;								//Return to main Menu
			//button_push=0;								//button check loop exit criteria
			scroll_ctrl=4;
			//Terminate USART Protocol here
			}

//		}

	
/*		
	button_push=1;										//resetting button check loop exit criteria

	if(sub_menu_scroll_ctrl<1)							//controlling scroll limits
		{
		sub_menu_scroll_ctrl=1;
		}

	else if(sub_menu_scroll_ctrl>1)
		{
		sub_menu_scroll_ctrl=1;
		}

*/	

	}												//End of Submenu

//********************************************************************************************************
}														//End of endless loop
return 0;
}
